//
//  AddressBook.m
//  OC05_NSArray_NSDictionary_NSSet
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 Rain. All rights reserved.
//

#import "AddressBook.h"
#import "Contact.h"
@implementation AddressBook

-(instancetype)initWithDictGroup:(NSMutableDictionary *)dictGroup{
    self = [super init];
    if(self){
        _dictGroup = dictGroup;
    }
    return self;
}

+(instancetype)AddressBookWithDictGroup:(NSMutableDictionary *)dictGroup{
    
    AddressBook *book = [[AddressBook alloc] initWithDictGroup:dictGroup];
    return book;
}


-(void)addContactWithCont:(Contact *)cont{
    //定义一个联系人对象
 //   NSMutableArray *letterOfArray = [NSMutableArray arrayWithCapacity:1];
    //取得联系人姓名的首字母
    NSString *key = [[cont.name substringWithRange:NSMakeRange(0, 1)] uppercaseString];
    NSMutableArray *letterOfArray = [NSMutableArray arrayWithCapacity:26];
    
    if (![self.dictGroup objectForKey:key]) {
        [letterOfArray addObject:cont];
        [self.dictGroup setObject:letterOfArray forKey:key];
    } else {
        [[self.dictGroup objectForKey:key] addObject:cont];
    }
    
    
    //以联系人的姓名首字母为键,对应的联系人对象为值,
//    [self.dictGroup addEntriesFromDictionary:@{key:[self.dictGroup objectForKey:key]}];
//    //将联系人对象存入数组中
//    [[self.dictGroup objectForKey:key] addObject:cont];
    
 //   [self.dictGroup setObject:letterOfArray forKey:key];
    
    
    
    
    
}


-(void)showContactWithKey:(NSString *)key{
  //  NSString *key = [cont.name substringWithRange:NSMakeRange(0, 1)];
    NSInteger a = [[self.dictGroup objectForKey:key] count];
    for(int i = 0;i < a ;i++){
        [[self.dictGroup objectForKey:key][i] contactMessage];
    }
}
-(void)showContactWithCont:(Contact *)cont{
    [cont contactMessage];
}
-(void)showContactWithAll{
    NSArray *dKeys = [self.dictGroup allKeys];
    for (NSInteger i = 0; i< dKeys.count; i++) {
        NSString *keys = [dKeys objectAtIndex:i];
        NSArray *value = [self.dictGroup objectForKey:keys];
        for(NSInteger j = 0;j < value.count;j++){
            [value[j] contactMessage];
        }
    }
}


@end
